package com.turing.usdtpay.mvp.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.turing.usdtpay.R;
import com.turing.usdtpay.adapter.StreamAdapter;
import com.turing.usdtpay.base.BaseFragment;
import com.turing.usdtpay.entity.CommissionData;
import com.turing.usdtpay.entity.CommissionItemData;
import com.turing.usdtpay.entity.StreamData;
import com.turing.usdtpay.mvp.contract.CommissionContract;
import com.turing.usdtpay.mvp.presenter.CommissionPresenter;
import com.turing.usdtpay.utils.DateUtils;
import com.turing.usdtpay.utils.LogUtils;
import com.turing.usdtpay.utils.ToastUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @作者：JAY
 * @创建时间：2019/10/15 21:54
 * @描述：
 **/
public class CommissionFragment extends BaseFragment implements CommissionContract.View ,SwipeRefreshLayout.OnRefreshListener {
    private RecyclerView mRecyclerView;
    private CommissionContract.Present mPresent;
    private ArrayList<StreamData> mList = new ArrayList<>();
    private StreamAdapter mAdapter;
    private int lastVisibleItem;
    private boolean hasMore = false;
    private int mCurrentPage = 1;
    private double mRate = 6.88;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    public static CommissionFragment newInstance(Bundle bundle){
        CommissionFragment fragment = new CommissionFragment();
        if (bundle != null){
            fragment.setArguments(bundle);
        }
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setPresenter(new CommissionPresenter(this));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_commission, container, false);
        initView(view);
        initRecyclerView();
        initTitle();
        mPresent.getData(getActivity(),mCurrentPage,10,true);
        return view;
    }

    private void initView(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorBlue);
        mSwipeRefreshLayout.setOnRefreshListener(this);
    }


    private void initTitle() {
        setTitle("佣金流水");
        showBackBtn(true);
        setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
            }
        });
        showTitleLogo(false);
    }

    private void initData(List<CommissionItemData> list) {
        for (int i=0;i < list.size();i++){
            StreamData data = new StreamData();
            CommissionItemData itemData = list.get(i);
            data.setCny((double)itemData.getNum()/100);
            data.setUsdt(((double)itemData.getNum()/100/ mRate));
            data.setTotal((double) itemData.getMoney()/100);
            if (itemData.getO_type() == 1){
                data.setName("买入USDT");
            }else {
                data.setName("卖出USDT");
            }
            data.setTime(DateUtils.dateToStrLong(new Date(itemData.getTime()*1000L)));
            mList.add(data);
        }
        LogUtils.d("add list.size -->"+mList.size());
    }

    private void initRecyclerView() {
        // 定义一个线性布局管理器
        final LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        // 设置布局管理器
        mRecyclerView.setLayoutManager(manager);
        LogUtils.d("add list "+mList.toString());
        mAdapter = new StreamAdapter(getActivity(),mList);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE){
                    if (lastVisibleItem +1 == mAdapter.getItemCount()){
                        if (hasMore){
                            mCurrentPage = ++mCurrentPage;
                            mPresent.getData(getActivity(),mCurrentPage,10,false);
                        }else {
                            ToastUtils.showNormolToast(getActivity(),"已经没有更多了");
                        }
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = manager.findLastVisibleItemPosition();
            }
        });
    }

    @Override
    public void onSuccess(CommissionData data, boolean fresh) {
        mSwipeRefreshLayout.setRefreshing(false);
        LogUtils.d("获取数据 -->"+data.toString());
        mRate = (double) data.getMoney_change_usdt()/100;
        if (fresh){
            mList.clear();
            mCurrentPage = 1;
            initData(data.getList_info());
            mAdapter.updateData(mList);
            mSwipeRefreshLayout.setRefreshing(false);
        }else {
            initData(data.getList_info());
            mAdapter.updateData(mList);
            if (data.getList_info().size() <1){
                hasMore = false;
            }else {
                hasMore = true;
            }
        }
    }

    @Override
    public void onFail(String msg) {
        mSwipeRefreshLayout.setRefreshing(false);
        ToastUtils.showNormolToast(getActivity(),msg);
    }

    @Override
    public void setPresenter(CommissionContract.Present presenter) {
        mPresent = presenter;
    }

    @Override
    public void onRefresh() {
        mPresent.getData(getActivity(),mCurrentPage,10,true);
    }
}
